package com.hdyg.sixg.fragment;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hdyg.sixg.R;
import com.hdyg.sixg.activity.WebActivity;
import com.hdyg.sixg.adapter.FMMainMenuAdapter;
import com.hdyg.sixg.entry.NativeGuideBean;
import com.hdyg.sixg.mvp.base.BasePresenter;
import com.hdyg.sixg.mvp.base.SpMsg;
import com.hdyg.sixg.util.DataCenter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.List;

import butterknife.BindView;

/**
 * @author CZB
 * @describe 应用
 * @time 2019/4/4 10:10
 */
public class ApplicationFragment extends BaseFragment {

    @BindView(R.id.ll_top_left)
    LinearLayout llTopLeft;
    @BindView(R.id.tvTopTitle)
    TextView tvTopTitle;
    @BindView(R.id.rv_main)
    RecyclerView rvMain;
    @BindView(R.id.srl_refresh)
    SmartRefreshLayout srlRefresh;

    private FMMainMenuAdapter mAdapter;
    private List<NativeGuideBean> menuDatas;


    @Override
    protected void createPresenter() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_recycleview;
    }

    @Override
    protected void initView() {
        tvTopTitle.setText(R.string.application_title);
        llTopLeft.setVisibility(View.GONE);
        rvMain.setLayoutManager(new GridLayoutManager(mContext, 3));
        menuDatas = DataCenter.getApplicationMenuData();
        onreFresh();
        initAdapter();
    }

    @Override
    protected void initData() {

    }

    private void onreFresh() {
        srlRefresh.setOnRefreshListener(refreshLayout -> {
            srlRefresh.finishRefresh(2000);//传入false表示刷新失败
        });
    }

    private void initAdapter() {
        mAdapter = new FMMainMenuAdapter(R.layout.item_fragment_main_menu, menuDatas);
        rvMain.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            if (menuDatas.get(position).getType() == NativeGuideBean.TYPE_1) {
                startActivity(menuDatas.get(position).getmClass());
            } else if (menuDatas.get(position).getType() == NativeGuideBean.TYPE_2) {
                mBundle = new Bundle();
                mBundle.putString(SpMsg.URL, menuDatas.get(position).getUrl());
                startActivity(WebActivity.class, mBundle);
            } else if (menuDatas.get(position).getType() == NativeGuideBean.TYPE_3) {
                toastNotifyShort(R.string.toast_development);
            }
        });
    }

}
